import 'package:flutter/material.dart';

import 'package:provider/provider.dart';
import 'package:recorder_flutter/flutter_sound/recording_provider.dart';

class RecordingScreenProvider extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final recordingProvider = Provider.of<RecordingProvider>(context);
    return SafeArea(
      child: Scaffold(
        body: SingleChildScrollView(
          child: Column(
            children: [
              ElevatedButton(
                onPressed: () async {
                  if (recordingProvider.isRecording) {
                    await recordingProvider.stopRecording();
                  } else {
                    await recordingProvider.startRecording(context);
                  }
                },
                child: Text(recordingProvider.isRecording
                    ? 'Stop Recording'
                    : 'Start Recording'),
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () async {
                  if (recordingProvider.isPlaying) {
                    await recordingProvider.stopPlayback();
                  } else {
                    // 这里播放最近录音的例子
                    if (recordingProvider.recordings.isNotEmpty) {
                      await recordingProvider.startPlayback(
                          recordingProvider.recordings.last.filePath);
                    }
                  }
                },
                child: Text(recordingProvider.isPlaying
                    ? 'Stop Playback'
                    : 'Start Playback'),
              ),
              TextButton(
                  onPressed: () async {
                    await recordingProvider.clearTemporaryDirectory();
                  },
                  child: Text('清除')),
              if (recordingProvider.recordingDuration != null)
                Text(
                    'Recording Duration: ${recordingProvider.recordingDuration!.inSeconds} seconds'),
              ListView.builder(
                physics: NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                itemCount: recordingProvider.recordings.length,
                itemBuilder: (BuildContext context, int index) {
                  Recording recording = recordingProvider.recordings[index];
                  return ListTile(
                    title: Text('Recording${index + 1}'),
                    subtitle:
                        Text('Duration:${recording.duration.inSeconds}seconds'),
                    onTap: () async {
                      if (recordingProvider.isPlaying) {
                        await recordingProvider.stopPlayback();
                      }
                      await recordingProvider.startPlayback(recording.filePath);
                    },
                  );
                },
              ),
              // FutureBuilder<int>(
              //   future: recordingProvider.getFileCountInTempDir(),
              //   builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
              //     if (snapshot.connectionState == ConnectionState.waiting) {
              //       return CircularProgressIndicator();
              //     } else if (snapshot.hasError) {
              //       return Text('Error:${snapshot.error}');
              //     } else {
              //       return Text('Files in temp directory: ${snapshot.data}');
              //     }
              //   },
              // ),
              Text('File length :${recordingProvider.fileCount}')
            ],
          ),
        ),
      ),
    );
  }
}
